package com.hl.icd.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageInfo;
import com.hl.icd.bean.ICD;
import com.hl.icd.bean.ICDHistory;
import com.hl.icd.bean.ResultVo;
import com.hl.icd.bean.Version;
import com.hl.icd.dao.ICDDao;
import com.hl.icd.service.ICDService;
import com.hl.icd.utils.ExcelUtil;
import com.hl.icd.utils.JwtUtil;
import com.hl.icd.utils.UUIDUtil;
import io.jsonwebtoken.Claims;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
@Api(tags = "ICD编码管理接口文档")
public class ICDController {

    @Autowired
    private ICDService icdService;

    @Autowired
    private ExcelUtil excelUtil;

    @Autowired
    private JwtUtil jwtUtil;

    @ApiOperation(
            value = "获取编码列表",
            notes = "根据页数和每页的数量分页获取编码列表",
            httpMethod = "GET",
            response = String.class)
    @GetMapping(value = "/codeQuery/{pageNum}/{pageSize}",produces="text/html;charset=UTF-8")
    public String codeQuery(@PathVariable("pageNum") int pageNum,
                            @PathVariable("pageSize") int pageSize,
                            ICD icd){
        String json = "";
        ObjectMapper om = new ObjectMapper();
        List<ICD> diseases = icdService.codeQuery(pageNum,pageSize,icd);
        Map<String,Object> resMap = new HashMap<>();
        PageInfo pageInfo = new PageInfo(diseases);
        String total = String.valueOf(pageInfo.getTotal());
        resMap.put("total",total);
        resMap.put("data",diseases);
        try {
            json = om.writeValueAsString(resMap);
            System.out.println(json);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return json;
    }

    @ApiOperation(
            value = "添加编码信息功能",
            notes = "根据前端提供的编码信息进行编码添加操作，此方法一般用在管理员对编码表的操作",
            httpMethod = "POST",
            response = String.class)
    @PostMapping(value = "/code/codeSave",produces="text/html;charset=UTF-8")
    public String codeSave(@RequestBody ICD icd){
        return icdService.insertOne(icd);
    }

    @ApiOperation(
            value = "删除一条编码数据",
            notes = "根据前端提供的编码id进行编码删除操作，此方法一般用在管理员对编码表的操作",
            httpMethod = "DELETE",
            response = String.class)
    @DeleteMapping(value = "/code/deleteOne/{id}",produces="text/html;charset=UTF-8")
    public String deleteOne(@PathVariable String id, HttpServletRequest request) throws Exception {
        String token = request.getHeader("token");
        Claims claims = jwtUtil.parseJWT(token);
        String editBy = (String) claims.get("id");
        return icdService.deleteOneById(id,editBy);
    }

    @ApiOperation(
            value = "批量删除编码功能",
            notes = "根据前端提供的编码id集合进行编码批量删除操作，此方法一般用在管理员对编码表的操作",
            httpMethod = "DELETE",
            response = String.class)
    @DeleteMapping(value = "/code/deleteBatch",produces="text/html;charset=UTF-8")
    public String deleteBatch(@RequestBody List<String> ids,HttpServletRequest request) throws Exception {
        String token = request.getHeader("token");
        Claims claims = jwtUtil.parseJWT(token);
        String editBy = (String) claims.get("id");
        return icdService.deleteBatch(ids,editBy);
    }

    @ApiOperation(
            value = "更新编码信息功能",
            notes = "根据前端提供的编码信息进行编码更新操作，此方法一般用在管理员对编码表的操作",
            httpMethod = "PUT",
            response = String.class)
    @PutMapping(value = "/code/codeUpdate",produces="text/html;charset=UTF-8")
    public String codeUpdate(@RequestBody ICD icd,HttpServletRequest request) throws Exception {
        String token = request.getHeader("token");
        Claims claims = jwtUtil.parseJWT(token);
        String editBy = (String) claims.get("id");
        return icdService.updateOne(icd,editBy);
    }

    @RequestMapping("/code/upload")
    public void uploadFile(MultipartFile file, HttpServletResponse response) throws Exception {
        System.out.println(file.getName());
        //解析excel文件
        List<List<Object>> rows = excelUtil.getBankListByExcel(file.getInputStream(),file.getOriginalFilename());
        ICD icd = new ICD();
        for (List<Object> row : rows) {
            icd.setId(UUIDUtil.getUUID());
            icd.setIcdCode((String) row.get(0));
            icd.setName((String) row.get(1));
            icd.setType((String) row.get(2));
            icdService.insertOne(icd);
        }
    }

    @GetMapping("/code/getVersionList")
    public List<Version> getVersionList(){
        return icdService.getVersionList();
    }

    @GetMapping(value = "/code/historyQuery/{pageNum}/{pageSize}",produces="text/html;charset=UTF-8")
    public String historyQuery(@PathVariable("pageNum") int pageNum,
                            @PathVariable("pageSize") int pageSize){
        String json = "";
        ObjectMapper om = new ObjectMapper();
        List<ICDHistory> histories = icdService.historyQuery(pageNum,pageSize);
        Map<String,Object> resMap = new HashMap<>();
        PageInfo pageInfo = new PageInfo(histories);
        String total = String.valueOf(pageInfo.getTotal());
        resMap.put("total",total);
        resMap.put("data",histories);
        try {
            json = om.writeValueAsString(resMap);
            System.out.println(json);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return json;
    }

}
